Imports System 
Imports System.Text 
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Configuration 
Imports System.Xml 
Imports System.Xml.Serialization
Imports SubSonic 
Imports SubSonic.Utilities
Namespace JPC.Medialibrary.DAL
    ''' <summary>
    ''' Controller class for PlaylistSongs
    ''' </summary>
		<System.ComponentModel.DataObject()> Public Partial Class PlaylistSongController
    
        ' Preload our schema..
        Dim thisSchemaLoad As PlaylistSong = New PlaylistSong()
        Private strUserName As String = String.Empty
        Protected ReadOnly Property UserName() As String
            Get
				If strUserName.Length = 0 Then
		        
    				If Not System.Web.HttpContext.Current Is Nothing Then
						strUserName = System.Web.HttpContext.Current.User.Identity.Name
					Else
		        		strUserName = System.Threading.Thread.CurrentPrincipal.Identity.Name
					End If
					Return strUserName
				End If
				Return strUserName
			End Get
        End Property
        <DataObjectMethod(DataObjectMethodType.Select, True)> Public Function FetchAll() As PlaylistSongCollection
        
            Dim coll As PlaylistSongCollection = New PlaylistSongCollection()
            Dim qry As Query = New Query(PlaylistSong.Schema)
            coll.LoadAndCloseReader(qry.ExecuteReader())
            Return coll
            
        End Function
        <DataObjectMethod(DataObjectMethodType.Select, True)> Public Function FetchByID(ByVal PlaylistId As Object) As PlaylistSongCollection 
        
            Dim coll As PlaylistSongCollection = New PlaylistSongCollection().Where("PlaylistId", PlaylistId).Load()
            Return coll
        
        End Function
        
        <DataObjectMethod(DataObjectMethodType.Select, True)> Public Function FetchByQuery(ByVal qry As SubSonic.Query) As PlaylistSongCollection 
        
            Dim coll As PlaylistSongCollection = New PlaylistSongCollection()
            coll.LoadAndCloseReader(qry.ExecuteReader())
            Return coll
        
        End Function
        <DataObjectMethod(DataObjectMethodType.Delete, True)> Public Function Delete(ByVal PlaylistId As Object) as Boolean
        
            Return (PlaylistSong.Delete(PlaylistId) = 1)
        
        End Function
        <DataObjectMethod(DataObjectMethodType.Delete, False)> Public Function Destroy(ByVal PlaylistId As Object) as Boolean
        
            Return (PlaylistSong.Destroy(PlaylistId) = 1)
        
        End Function
        
       
        <DataObjectMethod(DataObjectMethodType.Delete, True)> Public Function Delete(ByVal PlaylistId As Integer,ByVal SongId As Integer) as Boolean
            Dim qry As Query = new Query(PlaylistSong.Schema)
            qry.QueryType = QueryType.Delete
            qry.AddWhere("PlaylistId", PlaylistId).AND("SongId", SongId)
            qry.Execute()
            Return true
        End Function
       
    	
    	
	    ''' <summary>
	    ''' Inserts a record, can be used with the Object Data Source
	    ''' </summary>
        <DataObjectMethod(DataObjectMethodType.Insert, True)> Public Sub Insert(ByVal PlaylistId As Integer,ByVal SongId As Integer,ByVal Orden As Nullable(Of Integer))
	   
		    Dim item As PlaylistSong = New PlaylistSong()
		    
            item.PlaylistId = PlaylistId
            
            item.SongId = SongId
            
            item.Orden = Orden
            
	    
		    item.Save(UserName)
	   
	   End Sub
    	
	    ''' <summary>
	    ''' Updates a record, can be used with the Object Data Source
	    ''' </summary>
        <DataObjectMethod(DataObjectMethodType.Update, True)> Public Sub Update(ByVal PlaylistId As Integer,ByVal SongId As Integer,ByVal Orden As Nullable(Of Integer))
	    
		    Dim item As PlaylistSong = New PlaylistSong()
	        item.MarkOld()
	        item.IsLoaded = True
		    
			item.PlaylistId = PlaylistId 
				
			item.SongId = SongId 
				
			item.Orden = Orden 
				
	        item.Save(UserName)
	    End Sub
    End Class
End Namespace
